/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vastcm.stuhealth.client;

import com.vastcm.stuhealth.client.entity.BaseSetting;
import com.vastcm.stuhealth.client.entity.service.IBaseSettingService;
import com.vastcm.stuhealth.client.entity.service.core.ICoreService;
import com.vastcm.stuhealth.client.framework.ui.EditUI;
import com.vastcm.stuhealth.client.framework.ui.UIFrameworkUtils;
import com.vastcm.stuhealth.client.utils.ExceptionUtils;

import java.awt.Dimension;
import java.text.DecimalFormat;
import java.util.List;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import javax.swing.JSpinner.NumberEditor;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author house
 */
public class BaseSettingPanel extends EditUI<BaseSetting> {
    
    private Logger logger = LoggerFactory.getLogger(BaseSettingPanel.class);
    public static final String PROPERTY_PERIOD = "period";

    /**
     * Creates new form BaseSettingPanel
     */
    public BaseSettingPanel() {
        initComponents();
    }

    @Override
    public void onLoad() throws Exception {
        super.onLoad(); 
        initComponentsEx();
        initListeners();
    }
    
    protected void initListeners() {
        addPropertyChangeListener(PROPERTY_PERIOD, UIFrameworkUtils.getMainUI());
    }
    
    protected void initComponentsEx() {
    	setPreferredSize(new Dimension(280, 180));
    	((NumberEditor) spinYear.getEditor()).getTextField().setFormatterFactory(
    			new DefaultFormatterFactory(new NumberFormatter(new	DecimalFormat("#.#"))));
    	logger.info("....." + spinYear.getEditor().getClass().getName());
    }
    
    @Override
    public ICoreService<BaseSetting> getService() {
        return AppContext.getBean("baseSettingService", IBaseSettingService.class);
    }
    
    @Override
    public void loadData() {
        List<BaseSetting> ls = getService().getAll();
        if(ls != null && !ls.isEmpty()) {
            data = ls.get(0);
            spinYear.setValue(data.getYear());
            cbTerm.setSelectedIndex(data.getTerm()-1);
        }
    }

    @Override
    public void storeData() {
    	logger.info("year type=" + spinYear.getValue().getClass().getName());
    	data.setYear(((Number)spinYear.getValue()).intValue());
//        data.setYear((Integer)spinYear.getValue());
        data.setTerm(cbTerm.getSelectedIndex()+1);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btnSave = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        spinYear = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        cbTerm = new javax.swing.JComboBox();

        setLayout(new java.awt.BorderLayout());

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/system_save.gif"))); // NOI18N
        btnSave.setText("保存");
        btnSave.setFocusable(false);
        btnSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSave);

        add(jToolBar1, java.awt.BorderLayout.NORTH);

        jPanel1.setLayout(null);

        jLabel1.setText("年份");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 10, 80, 25);

        spinYear.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(2013), null, null, Integer.valueOf(1)));
        jPanel1.add(spinYear);
        spinYear.setBounds(90, 10, 120, 25);

        jLabel2.setText("学期");
        jLabel2.setVisible(false);
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 40, 80, 25);

        cbTerm.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2" }));
        cbTerm.setSelectedIndex(0);
        cbTerm.setVisible(false);
        jPanel1.add(cbTerm);
        cbTerm.setBounds(90, 40, 120, 25);

        add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        try {
            save();
            GlobalVariables.setIsDirty(true);
            firePropertyChange(PROPERTY_PERIOD, null, data);
        } 
        catch (Exception ex) {
            ExceptionUtils.writeExceptionLog(logger, ex);
            JOptionPane.showMessageDialog(this, "保存失败！详见日志。");
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbTerm;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JSpinner spinYear;
    // End of variables declaration//GEN-END:variables

    
}
